package com.mobileares.midp.widgets.client.textbox2;

import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.user.client.ui.Widget;
import com.mobileares.midp.widgets.client.textbox2.widget.ListBox;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-7-23
 * Time: 10:56:59
 * To change this template use File | Settings | File Templates.
 */
public class ListBoxLabel extends BaseLabelList<Object, ListBox> {
    private ListBox widget;

    public ListBoxLabel(String label) {
        this(label, true);
    }

    public ListBoxLabel(String label, boolean isNull) {
        super(label, isNull);
        widget = new ListBox();
        setLabelWidget(widget);
    }

    @Override
    protected void addItem(Object ob) {
        widget.addItem(codeConvertProvider.getText(ob), codeConvertProvider.getCode(ob));
    }

    @Override
    protected void addNullItem() {

    }

    public Object getValue() {
        if (widget.getValue() == null || widget.getValue().toString().trim().equals("")) return null;
        else
            return widget.getValue();
    }

    public void setValue(Object ob) {
        String oldValue = (String) getValue();
        String newValue = (ob == null ? "" : ob.toString());
        widget.setValue(newValue);
        
        changeValue(newValue, oldValue);
    }

    protected Widget getRealWidget() {
        return widget.getWidget();
    }

    @Override
    protected void fireErrorMessage() {
        widget.removeStyleDependentName("focus");
        widget.addStyleDependentName("error");

    }

    protected void fireRemoveErrorMessage() {
        widget.removeStyleDependentName("error");
    }

    @Override
    protected void fireFocus(FocusEvent event) {
        if (isValid())
            widget.removeStyleDependentName("error");
        widget.addStyleDependentName("focus");
    }

    @Override
    protected void fireBlur(BlurEvent event) {
        if (!isValid())
            widget.addStyleDependentName("error");
        widget.removeStyleDependentName("focus");

    }
}
